<?php require VIEWPATH. ADMIN_DIR. DIRECTORY_SEPARATOR. 'header.php'?>
<style type="text/css">
	a:hover{text-decoration: none;}
	.ace-icon.fa-clock-o{margin-right: 3px}
	
	.dropdown-menu li {
		width: 180px;
	}
	.content-filter {
		margin-top: 15px;
	}
</style>
<div class="page-header">
	<h1>
		班牌管理
		<!-- 教师权限不可添加 -->
		<?php if($rmxUser['user_type'] != USER_TYPE_TEACHER): ?>
			<a href="javascript:;" class="brand-update btn btn-info a-custom">
				<i class="ace-icon fa fa-send"></i> 批量更新设备版本
			</a>
			<a href="javascript:;" class="brand-restart btn btn-info a-custom">
				<i class="ace-icon fa fa-refresh"></i> 批量重启班牌
			</a>
			<span class="dropdown dropdown-custom">
				<div class="btn btn-info btn-dropdown"  data-toggle="dropdown">
					<i class="ace-icon fa fa-database"></i> 模板导入与导出&nbsp;&nbsp;<i class="ace-icon fa fa-caret-down"></i>
				</div>
				<ul class="dropdown-menu">
					<li>
						<a href="javascript:" id="import">
							<i class="ace-icon fa fa-upload"></i> 导入班牌所属班级数据
						</a>
					</li>
					<li>
						<!-- <a href="https://resource.rongmixiao.com/2021/09/06/9334fc34c3741bab1cc30cd0707d3d67.xls" target="_blank">
							<i class="ace-icon fa fa-download"></i> 导出班牌所属班级模板
						</a> -->
						<a id="export">
							<i class="ace-icon fa fa-download"></i> 导出班牌所属班级模板
						</a>
					</li>
				</ul>
			</span>
		<?php endif ?>
	</h1>
</div>

<div class="col-xs-12">
	<!-- 教师权限不可批量修改 -->
	<?php if($rmxUser['user_type'] != USER_TYPE_TEACHER): ?>
		<div>
			<i class="ace-icon fa fa-camera blue"></i> 默认摄像头缩放比例： 
			<a class="edit-zoom" href="#modal-zoom" data-toggle="modal" data-id="0" data-zoom="<?=$zoomConfig?>">
				<b class="red"><?=$zoomConfig?></b> <i class="fa fa-edit"></i>
			</a>
		</div>
		<!-- <div>
			<i class="ace-icon fa fa-clock-o blue"></i> 默认上课前多久进入考勤模式： 
			<a class="edit-sign-interval" href="#modal-sign-interval" data-toggle="modal" data-id="0" data-sign-interval="<?=$signIntervalConfig?>">
				<b class="red"><?= $signIntervalConfig ? $signIntervalConfig . '分钟' : '已关闭' ?></b> <i class="fa fa-edit"></i>
			</a>
		</div> -->
		<div>
			<i class="ace-icon fa fa-key blue"></i> 默认登录密码： 
			<a class="edit-sign-interval" href="#modal-brand-paw" data-toggle="modal" data-id="0">
				<b class="red"><?= $defaultBrandPaw ?></b> <i class="fa fa-edit"></i>
			</a>
		</div>
	<?php endif ?>

	<!-- <div class="mt10 mb10">
		<i class="ace-icon fa fa-clock-o blue"></i> 默认定时开关机时间：
		<a class="power-time" href="#modal-power-time" data-toggle="modal" data-id="0">
			<?php if($powertime): ?>
				<?php $powertimeJson = json_decode($powertime, true); ?>
				<?php foreach($powertimeJson as $time): ?>
					<span class="label label-success mr10"><?=$time['starttime']?> - <?=$time['endtime']?></span>
				<?php endforeach; ?>
			<?php else: ?>暂未设置<?php endif; ?>
			<i class="fa fa-edit"></i>
		</a>
	</div> -->

	<table id="dynamic-table" class="table table-striped table-bordered table-hover" style="margin-top:10px">
		<thead>
			<tr>
				<th>班牌ID</th>
				<!-- <th>所属校区</th> -->
				<th>所属班级</th>
				<th>在线状态</th>
				<th>登录密码</th>
				<th>摄像头缩放比例</th>
				<!-- <th>上课前多久进入考勤模式</th> -->
				<!-- <th>定时开关机时间</th> -->
				<!-- <th>对话留言内容发送</th> -->
				<th>版本信息</th>
				<th>最后上线时间</th>
				<th>最后下线时间</th>
				<th></th>
			</tr>
		</thead>
		<tbody>
		</tbody>
	</table>
</div>

<!-- 班牌弹窗 -->
<div id="modal-brand" class="modal fade">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header no-padding">
				<div class="table-header">
					<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
						<span class="white">&times;</span>
					</button>
					<span id="modal-name">班牌管理</span>
				</div>
			</div>
			<div class="modal-body no-padding">
				<div class="row">
					<div class="col-xs-12">
						<form class="form-horizontal mt10" id="brand-form">
							<input type="hidden" name="id" />
							<div class="form-group">
								<label class="col-sm-3 control-label no-padding-right">班牌ID：</label>
								<div class="col-sm-6">
									<span class="form-control no-border no-padding-left" id="modal-brand-id"></span>
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-3 control-label no-padding-right">登录密码：</label>
								<div class="col-sm-6">
									<input type="text" name="brand_password" class="form-control" placeholder="请填写班牌登录密码" />
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-3 control-label no-padding-right">对话留言内容发送：</label>
								<div class="col-sm-6">
									<select name="allow_send" class="form-control">
										<option value="1">允许发送文字内容</option>
										<option value="0">关闭</option>
									</select>
								</div>
							</div>
						</form>
					</div>
				</div>
			</div>
			<div class="modal-footer no-margin-top" >
				<a href="javascript:;" class="btn btn-sm btn-primary" id="save-brand">
					<i class="ace-icon fa fa-check"></i> 保存
				</a>
			</div>
		</div>
	</div>
</div>

<!-- 定时开关机弹窗 -->
<div id="modal-power-time" class="modal fade">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header no-padding">
				<div class="table-header">
					<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
						<span class="white">&times;</span>
					</button>
					<span>设置定时开关机</span>
				</div>
			</div>
			<div class="modal-body no-padding">
				<div class="row">
					<div class="col-xs-12">
						<form class="form-horizontal mt10">
							<input type="hidden" name="id" />
							<div class="form-group mb10">
								<a href="javascript:;" class="col-sm-4 control-label bolder blue create-power-time"><i class="fa fa-plus"></i> 添加定时开关机时间</a>
							</div>
							<div id="power-time-div"></div>
						</form>
					</div>
				</div>
			</div>
			<div class="modal-footer no-margin-top" >
				<a href="javascript:;" class="btn btn-sm btn-primary" id="save-power-time">
					<i class="ace-icon fa fa-check"></i> 保存
				</a>
			</div>
		</div>
	</div>
</div>

<!-- 摄像头缩放比例弹窗 -->
<div id="modal-zoom" class="modal fade">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header no-padding">
				<div class="table-header">
					<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
						<span class="white">&times;</span>
					</button>
					<span>缩放比例管理</span>
				</div>
			</div>
			<div class="modal-body no-padding">
				<div class="row">
					<div class="col-xs-12">
						<form class="form-horizontal mt10" id="zoom-form">
							<input type="hidden" name="id" />
							<div class="form-group">
								<label class="col-sm-3 control-label no-padding-right">班牌ID：</label>
								<div class="col-sm-6">
									<span class="form-control no-border no-padding-left" id="zoom-brand-name"></span>
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-3 control-label no-padding-right">缩放比例：</label>
								<div class="col-sm-6">
									<input type="text" name="camera_zoom" class="form-control" readonly="" value="" />
									<div class="help-block" id="zoom-slider"></div>
								</div>
							</div>
						</form>
					</div>
				</div>
			</div>
			<div class="modal-footer no-margin-top" >
				<a href="javascript:;" class="btn btn-sm btn-primary" id="save-zoom">
					<i class="ace-icon fa fa-check"></i> 保存
				</a>
			</div>
		</div>
	</div>
</div>

<!-- 上课前多久进入考勤模式弹窗 -->
<div id="modal-sign-interval" class="modal fade">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header no-padding">
				<div class="table-header">
					<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
						<span class="white">&times;</span>
					</button>
					<span>上课前多久进入考勤模式（0 即是关闭）</span>
				</div>
			</div>
			<div class="modal-body no-padding">
				<div class="row">
					<div class="col-xs-12">
						<form class="form-horizontal mt10" id="sign-interval-form">
							<input type="hidden" name="id" />
							<div class="form-group">
								<label class="col-sm-3 control-label no-padding-right">班牌ID：</label>
								<div class="col-sm-6">
									<span class="form-control no-border no-padding-left" id="sign-interval-brand-name"></span>
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-3 control-label no-padding-right">分钟：</label>
								<div class="col-sm-6">
									<input type="number" name="sign-interval" class="form-control" value="" />
								</div>
							</div>
						</form>
					</div>
				</div>
			</div>
			<div class="modal-footer no-margin-top" >
				<a href="javascript:;" class="btn btn-sm btn-primary" id="save-sign-interval">
					<i class="ace-icon fa fa-check"></i> 保存
				</a>
			</div>
		</div>
	</div>
</div>

<!-- 默认班牌密码弹窗 -->
<div id="modal-brand-paw" class="modal fade">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header no-padding">
				<div class="table-header">
					<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
						<span class="white">&times;</span>
					</button>
					<span>默认登录密码管理</span>
				</div>
			</div>
			<div class="modal-body no-padding">
				<div class="row">
					<div class="col-xs-12">
						<form class="form-horizontal mt10">
							<div class="form-group">
								<label class="col-sm-3 control-label no-padding-right">默认登录密码：</label>
								<div class="col-sm-6">
									<input type="text" name="default-brand-paw" class="form-control" value="<?= $defaultBrandPaw ?>" />
								</div>
							</div>
						</form>
					</div>
				</div>
			</div>
			<div class="modal-footer no-margin-top" >
				<a href="javascript:;" class="btn btn-sm btn-primary" id="save-default-brand-paw">
					<i class="ace-icon fa fa-check"></i> 保存
				</a>
			</div>
		</div>
	</div>
</div>

<!-- 默认班牌密码弹窗 -->
<div id="modal-allow-send" class="modal fade">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header no-padding">
				<div class="table-header">
					<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
						<span class="white">&times;</span>
					</button>
					<span>批量设置对话留言内容发送</span>
				</div>
			</div>
			<div class="modal-body no-padding">
				<div class="row">
					<div class="col-xs-12">
						<form class="form-horizontal mt10">
							<div class="form-group">
								<label class="col-sm-3 control-label no-padding-right">对话留言内容发送：</label>
								<div class="col-sm-6">
									<select name="batch-allow-send" class="form-control">
										<option value="1">允许发送文字内容</option>
										<option value="0">关闭</option>
									</select>
								</div>
							</div>
						</form>
					</div>
				</div>
			</div>
			<div class="modal-footer no-margin-top" >
				<a href="javascript:;" class="btn btn-sm btn-primary" id="save-batch-allow-send">
					<i class="ace-icon fa fa-check"></i> 保存
				</a>
			</div>
		</div>
	</div>
</div>

<!-- 修改所属班级弹窗 -->
<div id="modal-brand-class" class="modal fade">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header no-padding">
				<div class="table-header">
					<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
						<span class="white">&times;</span>
					</button>
					<span id="modal-name">修改所属班级</span>
				</div>
			</div>
			<div class="modal-body no-padding">
				<div class="row">
					<div class="col-xs-12">
						<form class="form-horizontal mt10" id="brand-class-form">
							<input type="hidden" name="id" />
							<div class="form-group">
								<label class="col-sm-3 control-label no-padding-right">班牌ID：</label>
								<div class="col-sm-6">
									<span class="form-control no-border no-padding-left" id="brand-id"></span>
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-3 control-label no-padding-right">关联班级：</label>
								<div class="col-sm-6">

									<select class="chosen-select form-control tag-input-style" id="form-field-select-3" data-placeholder="选择班牌关联的班级" name="class_id">
										<?php foreach($classList as $class): ?>
											<option value="<?=$class['id']?>"><?=$class['class_name']?></option>
										<?php endforeach; ?>
									</select>
								</div>
							</div>
						</form>
					</div>
				</div>
			</div>
			<div class="modal-footer no-margin-top" >
				<a href="javascript:;" class="btn btn-sm btn-primary" id="save-brand-class">
					<i class="ace-icon fa fa-check"></i> 保存
				</a>
			</div>
		</div>
	</div>
</div>

<?php require VIEWPATH. ADMIN_DIR. DIRECTORY_SEPARATOR. 'footer.php'?>
<script src="https://channel.sinaapp.com/api.js"></script>
<script type="text/javascript">
	var brandId = 0;

	$(function(){
		// 连接到socket
        socket = new WebSocket('<?= BASE_SOCKET_LINK ?>');
        socket.onopen = function(evt) {
			console.log('连接socket成功');
			var data = {'type': 0, 'channel_id': "<?= $channelId?>"};
			// var data = {'type': 0, 'channel_id': "10147"};
            var json = JSON.stringify(data);
            socket.send(json);
		};
console.log(11);
		socket.onmessage = function(res) {
            console.log('onmessage');
            console.log(res.data);

			layer.closeAll('loading');

			var socketData = JSON.parse(res.data);
			// 如果是截图类型的数据，就返回大图
			if(socketData.type == '<?=CONTENT_TYPE_SCREENSHOT?>') {

				var imageUrl = typeof socketData.content != "undefined" ? socketData.content : socketData.msg;
				$.colorbox({
					href: imageUrl,
					width: "60%"
				});
			}
			// 如果是获取版本号
			if(socketData.type == "<?=CONTENT_TYPE_GETVERSION?>") {
				if(typeof socketData.msg != 'undefined') {
					var versionData = socketData.msg
				} else {
					var versionData = JSON.parse(socketData.content);
				}
				tips_alert("当前版本名：" + versionData.versionName + "<br/>当前版本号：" + versionData.versionCode, true);
			}
			// 如果是获取本地缓存数据
			if(socketData.type == "<?=CONTENT_TYPE_SIGN_DATA?>") {
				if(typeof socketData.msg != 'undefined') {
					var versionData = socketData.msg
				} else {
					var versionData = JSON.parse(socketData.content);
				}
				console.log(versionData);
			}
			// 如果是请求远程关机
			if(socketData.type == "<?=CONTENT_TYPE_POWEROFF?>") {
				if(typeof socketData.msg != 'undefined') {
					var versionData = socketData.msg
				} else {
					var versionData = JSON.parse(socketData.content);
				}
				if(versionData === false){
					tips_alert('当前系统无root权限，需要刷机获取', false);
				} else {
					tips_alert('发送远程关机请求成功', true);
				}
			}
			// 如果是请求远程重启
			if(socketData.type == "<?=CONTENT_TYPE_POWERRESTART?>") {
				if(typeof socketData.msg != 'undefined') {
					var versionData = socketData.msg
				} else {
					var versionData = JSON.parse(socketData.content);
				}
				if(versionData === false){
					tips_alert('当前系统无root权限，需要刷机获取', false);
				} else {
					tips_alert('发送远程重启请求成功', true);
				}
			}
		}

		// 班牌表格
		$('#dynamic-table').dt({
			"ajaxUrl": '<?=admin_url('brand/get_brand_list')?>',
			"columns": [
				{"data": function(ret){
					if(ret.type == "<?= BRAND_TYPE_NORMAL ?>") {
						return ret.id;
					} else {
						return ret.id + '<span class="label label-danger mr10" style="margin-left: 20px;">'+ ret.typeName +'</span>';
					}
				}},
				// {"data": "campusName"},
				{"data": function(ret){
					return '<a class="edit-brand-class" href="#modal-brand-class" data-toggle="modal" data-id="'+ ret.id +'" data-classid="'+ ret.class_id +'">'+ ret.className +'  <i class="fa fa-edit"></i></a>';
				}},
	            {"data": function(ret){
	            	var onlineState = '<span class="green"><i class="fa fa-circle"></i> 在线</span>';
	            	if(ret.is_online == 0) onlineState = '<span class="light-grey"><i class="fa fa-circle"></i> 离线</span>';
	            	return onlineState;
				}},
				{"data": function(ret){
					if(ret.isDefaultPaw) {
						return '<span class="text-primary">' + ret.brand_password + '</span>';
					} else {
						return '<span class="text-danger">' + ret.brand_password + '</span>';
					}
				}},
	            {"data": function(ret){
					if(ret.isDefaultZoom) {
						return '<a class="edit-zoom" href="#modal-zoom" data-toggle="modal" data-id="'+ ret.id +'" data-zoom="'+ ret.brand_camera_zoom +'">'+ ret.brand_camera_zoom +'  <i class="fa fa-edit"></i></a>';
					} else {
						return '<a class="edit-zoom text-danger" href="#modal-zoom" data-toggle="modal" data-id="'+ ret.id +'" data-zoom="'+ ret.brand_camera_zoom +'">'+ ret.brand_camera_zoom +'  <i class="fa fa-edit"></i></a>';
					}
	            }},
				// {"data": function(ret){
				// 	if(ret.isDefaultInterval) {
				// 		if(ret.sign_interval == 0) {
				// 			return '<a class="edit-sign-interval" href="#modal-sign-interval" data-toggle="modal" data-id="'+ ret.id +'" data-sign-interval="'+ ret.sign_interval +'"><span class="red">已关闭</span>  <i class="fa fa-edit"></i></a>';
				// 		} else {
				// 			return '<a class="edit-sign-interval" href="#modal-sign-interval" data-toggle="modal" data-id="'+ ret.id +'" data-sign-interval="'+ ret.sign_interval +'">'+ ret.sign_interval +'分钟 <i class="fa fa-edit"></i></a>';
				// 		}
				// 	} else {
				// 		if(ret.sign_interval == 0) {
				// 			return '<a class="edit-sign-interval" href="#modal-sign-interval" data-toggle="modal" data-id="'+ ret.id +'" data-sign-interval="'+ ret.sign_interval +'"><span class="red">已关闭</span>  <i class="fa fa-edit"></i></a>';
				// 		} else {
				// 			return '<a class="edit-sign-interval text-danger" href="#modal-sign-interval" data-toggle="modal" data-id="'+ ret.id +'" data-sign-interval="'+ ret.sign_interval +'">'+ ret.sign_interval +'分钟 <i class="fa fa-edit"></i></a>';
				// 		}
				// 	}
	            // }},
	            // {"data": function(ret){
	            // 	var powerStr = "";
	            // 	if(ret.power_time == '<?=$powertime?>') {
	            // 		powerStr = '<a class="power-time text-success" href="#modal-power-time" data-toggle="modal" data-id="'+ ret.id +'">跟随默认时间 <i class="fa fa-edit"></i></a>';
	            // 	} else {
	            // 		powerStr = '<a class="power-time text-danger" href="#modal-power-time" data-toggle="modal" data-id="'+ ret.id +'">自定义时间 <i class="fa fa-edit"></i></a>';
	            // 	}
	            // 	return powerStr;
				// }},
				// {"data": "allow_send_chinese"},
				{"data": "version_name"},
	            {"data": "online_time"},
	            {"data": "offline_time"},
	            {"data": function(ret){
	            	// 教师权限不可批量修改
					<?php if($rmxUser['user_type'] == USER_TYPE_TEACHER): ?>
						return '';
					<?php endif ?>	
	            	return '<div class="btn-group">\
						<button data-toggle="dropdown" aria-expanded="false" class="btn btn-sm btn-success dropdown-toggle">\
							<i class="fa fa-gear"></i> 班牌管理 <i class="ace-icon fa fa-angle-down icon-on-right"></i>\
						</button>\
						<ul class="dropdown-menu dropdown-danger">\
							<li>\
								<a class="brand_manage" href="#modal-brand" data-toggle="modal" data-id="'+ ret.id +'" data-password="'+ ret.brand_password +'" data-allow-send="' + ret.allow_send + '"><i class="fa fa-gear"></i> 班牌管理</a>\
							</li>\
							<li class="divider"></li>\
							<li>\
								<a class="get-version" href="javascript:;" data-id="'+ ret.id +'"><i class="fa fa-link"></i> 获取设备版本</a>\
							</li>\
							<li>\
								<a class="screen-shot" href="javascript:;" data-id="'+ ret.id +'"><i class="fa fa-image"></i> 实时截图</a>\
							</li>\
							<li class="divider"></li>\
							<li>\
								<a class="power-restart" href="javascript:;" data-id="'+ ret.id +'"><i class="fa fa-refresh"></i> 远程重启</a>\
							</li>\
							<li class="divider"></li>\
							<li>\
								<a class="update-version" href="javascript:;" data-id="'+ ret.id +'"><i class="fa fa-send"></i> 更新设备版本</a>\
							</li>\
						</ul>\
					</div>';
	            }},
			]
		});
		// 自定义项显示红色
		// setTimeout(() => {
		// 	$(".edit-zoom").each(function() {
		// 	console.log($(this).data('zoom'));
		// });
		// }, 2000);

		// 显示弹窗
		var form = $("#brand-form");
		$("body").on("click", ".brand_manage", function(){
			var id = $(this).data("id");
			form.find("[name=id]").val(id);
			$("#modal-brand-id").text(id);
			form.find("[name=brand_password]").val($(this).data("password"));
			form.find("[name=allow_send]").val($(this).data("allow-send"));
		})
		// 提交表单
		$("#save-brand").click(function(){
			form.submit();
		})
		var submitUrl = "<?=admin_url('brand/save_brand_action')?>";
		form_submit(form, submitUrl, function(res) {
			tips_alert(res.msg, res.success, function(){
				if(res.success) window.location.reload();
			});
		})

		// 发送截屏请求
		$("body").on("click", ".screen-shot", function(){
			var id = $(this).data("id");
			var data = {
				"brand": id,
				"channel": "<?=$channelId?>",
				"noHideLoading": true,			// 请求结束不隐藏loading
			};
			var getUrl = "<?=admin_url('brand/screen_shot')?>";
			ajax_get(getUrl, data, function(res){
			}, function(res) {
				layer.closeAll('loading');
			})
		})

		// 发送退出应用请求
		$("body").on("click", ".quit", function(){
			var id = $(this).data("id");
			layer.confirm("确定退出应用？", function() {
				var data = {
					"brand": id,
					"channel": "<?=$channelId?>"
				};
				var getUrl = "<?=admin_url('brand/quit')?>";
				ajax_get(getUrl, data);
			})	
		})

		// 发送关机请求
		$("body").on("click", ".power-off", function(){
			var id = $(this).data("id");
			layer.confirm("确定关机？", function() {
				var data = {
					"brand": id,
					"channel": "<?=$channelId?>",
				};
				var getUrl = "<?=admin_url('brand/power_off')?>";
				ajax_get(getUrl, data);
			})
		})

		// 发送重启请求
		$("body").on("click", ".power-restart", function(){
			var id = $(this).data("id");
			layer.confirm("确定重启该班牌？", function() {
				var data = {
					"brand": id,
					"channel": "<?=$channelId?>",
				};
				var getUrl = "<?=admin_url('brand/power_restart')?>";
				ajax_get(getUrl, data);
			})
		})

		// 新增开关机时间
		$("body").on("click", ".create-power-time", function(){
			create_power_time_item("8:00", "18:00");
		})
		// 设置定时开关机时间
		$("body").on("click", ".power-time", function(){
			brandId = $(this).data("id");
			// 获取设备开关机时间
			var params = {brand: brandId};
			var getUrl = "<?=admin_url('brand/get_power_time')?>";
			ajax_get(getUrl, params, function(res) {
				// 清空时间点
				$("#power-time-div").empty();

				for(var i in res.data) {
					var row = res.data[i];
					create_power_time_item(row.starttime, row.endtime);
				}
			});
		})
		// 保存开关机时间
		$("#save-power-time").click(function(){
			var powerTime = new Array();
			$("#power-time-div .form-group").each(function(){
				// 检查内容是否被删除
				if($(this).find(".time-start").length) {
					var timeData = {
						"starttime" : $(this).find(".time-start").text(),
						"endtime": $(this).find(".time-end").text()
					}
					powerTime.push(timeData);
				}
			})

			// 提交到后台
			var params = {
				"brand": brandId,
				"time": powerTime
			};
			var actionUrl = "<?=admin_url('brand/save_power_time')?>";
			ajax_post(actionUrl, params, function(res){
				window.location.reload();
			})
		})
		// 获取设备版本
		$("body").on("click", ".get-version", function(){
			var id = $(this).data("id");
			var data = {
				"brand": id,
				"channel": "<?=$channelId?>",
			};
			var getUrl = "<?=admin_url('brand/get_version')?>";
			ajax_get(getUrl, data);
		})

		// 更新设备版本
		$("body").on("click", ".update-version", function(){
			var id = $(this).data("id");
			layer.confirm("确定更新设备版本？", function() {
				var data = {
					"brand": id,
					"channel": "<?=$channelId?>",
				};
				var getUrl = "<?=admin_url('brand/update_version')?>";
				ajax_get(getUrl, data);
			})
		})

		// 获取设备本地考勤数据
		$("body").on("click", ".get-sign-data", function(){
			var id = $(this).data("id");
			var data = {
				"brand": id,
				"channel": "<?=$channelId?>",
			};
			var getUrl = "<?=admin_url('brand/get_sign_data')?>";
			ajax_get(getUrl, data);
		})


		// 编辑摄像头缩放比例
		var zoomForm = $("#zoom-form");
		$("body").on("click", ".edit-zoom", function(){
			var id = $(this).data("id");
			var zoomName = id == 0 ? '全部班牌' : id;
			var zoom = $(this).data("zoom");

			// 弹窗赋值
			$("#zoom-brand-name").text(zoomName);
			zoomForm.find("[name=id]").val(id);
			zoomForm.find("[name=camera_zoom]").val(zoom);

			$("#zoom-slider").slider({
				value: zoom,
				range: "min",
				min: 0.1,
				max: 1,
				step: 0.1,
				slide: function( event, ui ) {
					$("[name=camera_zoom]").val(ui.value);
				}
			});
		})
		// 提交表单
		$("#save-zoom").click(function(){
			zoomForm.submit();
		})
		form_submit(zoomForm, "<?=admin_url('brand/save_brand_zoom_action')?>", function(res) {
			tips_alert(res.msg, res.success, function(){
				if(res.success) window.location.reload();
			});
		})

		// 上课前多久进入考勤模式
		var signIntervalForm = $("#sign-interval-form");
		$("body").on("click", ".edit-sign-interval", function(){
			var id = $(this).data("id");
			var signIntervalName = id == 0 ? '全部班牌' : id;
			var signInterval = $(this).data("sign-interval");
			// 弹窗赋值
			$("#sign-interval-brand-name").text(signIntervalName);
			signIntervalForm.find("[name=id]").val(id);
			signIntervalForm.find("[name=sign-interval]").val(signInterval);
		})
		// 提交表单
		$("#save-sign-interval").click(function(){
			signIntervalForm.submit();
		})
		form_submit(signIntervalForm, "<?=admin_url('brand/save_brand_sign_interval_action')?>", function(res) {
			tips_alert(res.msg, res.success, function(){
				if(res.success) window.location.reload();
			});
		});
		// 推送班牌更新
		$("body").on("click", ".brand-update", function() {
			layer.confirm("确定批量推送更新？", function() {
				var url = "<?= admin_url('brand/notify_brand_update') ?>";
				var data = {
					"channel": "<?=$channelId?>"
				};
				ajax_get(url, data, function() {
					
				});
			});
		});

		// 批量重启班牌
		$("body").on("click", ".brand-restart", function() {
			layer.confirm("确定批量重启班牌？", function() {
				var url = "<?= admin_url('brand/notify_brand_restart') ?>";
				var data = {
					"channel": "<?=$channelId?>"
				};
				ajax_get(url, data, function() {
					
				});
			});
		});

		// 保存默认登录密码
		$("#save-default-brand-paw").click(function(){
			var defaultPaw = $("[name=default-brand-paw]").val();
			// 提交到后台
			var params = {
				"default_brand_paw": defaultPaw,
			};
			var actionUrl = "<?=admin_url('brand/save_default_brand_paw')?>";
			ajax_post(actionUrl, params, function(res){
				window.location.reload();
			})
		});

		// 批量设置对话留言内容发送
		$("#save-batch-allow-send").click(function(){
			var val = $("[name=batch-allow-send]").val();
			// 提交到后台
			var params = {
				"allow_send": val,
			};
			var actionUrl = "<?=admin_url('brand/save_batch_allow_send')?>";
			ajax_post(actionUrl, params, function(res){
				window.location.reload();
			})
		});

		// 显示修改对应班级弹窗
		var brandClassForm = $("#brand-class-form");
		$("body").on("click", ".edit-brand-class", function(){
			brandClassForm.find("[name=class_id]").val($(this).data("classid"));
			brandClassForm.find("[name=id]").val($(this).data("id"));
			$("#brand-id").text($(this).data("id"));;
			// setTimeout(function(){
			// 	$('.chosen-select').chosen({
			// 		search_contains: true
			// 	}); 
			// }, 200)
		})
		// 保存班牌对应班级
		$("#save-brand-class").click(function(){
			var id = brandClassForm.find("[name=id]").val();
			var classId = brandClassForm.find("[name=class_id]").val();
			var data = {
				'id': id,
				'class_id': classId
			}
			var actionUrl = "<?=admin_url('brand/save_brand_class_action')?>";
			ajax_post(actionUrl, data, function(res){
				window.location.reload();
			})
		})

		// 导出班牌班级数据
		$("#export").click(function() {
			$url = "<?=admin_url('brand/export_brand_class')?>";
			window.location.href  = $url;
		});

		// 导入班牌所属班级
		var upFromurl = '<?=UP_FORM_URL?>';
		var signature = '<?=$signature?>';
		var policy = '<?=$policy?>';
		$("#import").uploader({
			"server": upFromurl,
			"authorization": signature,
			"policy": policy,
			// 只支持上传 xls
			"accept": "xls",
			"callback": function(response) {
				// 上传后的文件名
				var fileLink = upyunUrl + response.url;
				ajax_post("<?= admin_url('import/import_brand_class') ?>", { fileLink: fileLink}, function(res) {
					// 刷新页面
					location.reload();
				});
			}
		});
		// 设置选择文件按钮大小
		setTimeout(function(){
			$(".webuploader-pick").next().width(158).height(32);
		}, 100);
	})

	// 生成定时开关机时间
	function create_power_time_item(starttime, endtime) {
		$("#power-time-div").append('<div class="form-group mg0">\
			<div class="col-sm-1"></div>\
			<div class="alert alert-success col-sm-10">\
				<button type="button" class="close" data-dismiss="alert">\
					<i class="ace-icon fa fa-times"></i>\
				</button>\
				<a href="javascript:;" class="timepicker time-start" data-default-time="'+ starttime +'">'+ starttime +'</a> - <a href="javascript:;" class="timepicker  time-end" data-default-time="'+ endtime +'">'+ endtime +'</a>\
			</div>\
		</div>');

		$('.timepicker').timepicker({
				minuteStep: 1,
				showSeconds: false,
				showMeridian: false,
				disableFocus: true,
				icons: {
					up: 'fa fa-chevron-up',
					down: 'fa fa-chevron-down'
				}
			}).on('changeTime.timepicker', function(e) {
				var target = e.currentTarget;
				$(target).text(e.time.value)
			});
	}
</script>